package fr.oz.mobile.module.profile.day 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import fr.digitas.flowearth.conf.Conf;
	import fr.oz.core.datas.ProfilListDatas;
	import fr.oz.core.events.ClockEvent;
	import fr.oz.core.utils.Utils;
	import fr.oz.mobile.common.ui.SelectButton;
	import fr.oz.mobile.module.IView;
	import fr.oz.view.profile.dragable.DragableClock;
	import fr.oz.view.profile.dragable.ui.HourViewer;
	/**
	 * ...
	 * @author Fazermokeur
	 */
	public class HourSelector extends Sprite implements IView
	{
		private var setButton:SelectButton;
		
		private var profil:ProfilListDatas;
		private var timeViewer:HourViewer;
		private var clock:DragableClock;
		
		public function HourSelector(_profil:ProfilListDatas) 
		{
			addEventListener(Event.ADDED_TO_STAGE, init);
			
			profil = _profil;
			
			setButton = new SelectButton();
			addChild(setButton);
		}
		
		private function init(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			addEventListener(Event.REMOVED_FROM_STAGE, dispose);
			
			timeViewer = new HourViewer();
			timeViewer.y = 155;
			timeViewer.x = (stage.stageWidth >> 1) >> 0;
			addChild(timeViewer);
			
			clock = new DragableClock(profil);
			clock.addEventListener(ClockEvent.CLOCK_RELEASE, refreshTimerViewer);
			addChild(clock);
			clock.y = 460;
			clock.x = stage.stageWidth >> 1;
			
			setButton.y = stage.stageHeight - 90;
			setButton.x = (stage.stageWidth - setButton.width) >> 1;
			setButton.addEventListener(MouseEvent.CLICK, handleClick);
		}
		
		private function refreshTimerViewer(e:ClockEvent):void 
		{
			timeViewer.setHour(e.hour, e.minut);
		}
		
		private function handleClick(e:MouseEvent):void 
		{
			profil.time_hour = Utils.addZero("" + clock.hour);
			profil.time_minut = Utils.addZero("" + clock.minut);
			trace("handleCLCIK SET BUTTON");
			dispatchEvent(new Event(Event.COMPLETE));
		}
		
		private function dispose(e:Event):void 
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, dispose);
			
			removeChild(clock);
			clock.removeEventListener(ClockEvent.CLOCK_RELEASE, refreshTimerViewer);
			clock = null;
			
			removeChild(timeViewer);
			timeViewer = null;
		}
		
		/* INTERFACE fr.oz.mobile.module.IView */
		
		public function getValue():ProfilListDatas
		{
			return profil;
		}
		
		public function removeView():void
		{
			
		}
		
		public function activeView():void
		{
			
		}
		
		public function get title():XMLList
		{
			return Conf.getDatas("wakeUpProfile").data.ui.wakeUpTime;
		}
		
		public function get cachePosY():int
		{
			return 540;
		}
		
	}

}